package chixing.day13.work;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 消费记录类
class Consumption {
    private String brand;  // 品牌
    private double cost;   // 单次消费金额

    public Consumption(String brand, double cost) {
        this.brand = brand;
        this.cost = cost;
    }

    public String getBrand() {
        return brand;
    }

    public double getCost() {
        return cost;
    }
}

public class CostSum {
    public static void main(String[] args) {
        // 1. 存储多条消费记录（包含同一品牌的多次消费）
        List<Consumption> records = new ArrayList<>();
        records.add(new Consumption("苹果", 5999.0));  // 手机
        records.add(new Consumption("华为", 4999.0));  // 手机
        records.add(new Consumption("苹果", 999.0));   // 耳机
        records.add(new Consumption("小米", 1999.0));  // 平板
        records.add(new Consumption("华为", 2999.0));  // 手表
        records.add(new Consumption("联想", 6999.0));  // 电脑
        records.add(new Consumption("小米", 399.0));   // 手环

        // 2. 使用HashMap统计各品牌总费用
        Map<String, Double> totalCostMap = new HashMap<>();

        for (Consumption record : records) {
            String brand = record.getBrand();
            double cost = record.getCost();

            // 判断品牌是否已存在于Map中
            if (totalCostMap.containsKey(brand)) {
                // 已存在则累加费用
                totalCostMap.put(brand, totalCostMap.get(brand) + cost);
            } else {
                // 不存在则新增记录
                totalCostMap.put(brand, cost);
            }
        }

        // 3. 打印统计结果
        System.out.println("各品牌总费用统计：");
        for (Map.Entry<String, Double> entry : totalCostMap.entrySet()) {
            System.out.println(entry.getKey() + "：" + entry.getValue() + "元");
        }
    }
}

